using KinonekoSoftware.CodariaDB.Documents.Timelines;

namespace KinonekoSoftware.Studio.Pages.Notions
{
    partial class PlotsQuickEditorViewModel
    {
        
        //-------------------------------------------------------------
        //
        //          Fields
        //
        //-------------------------------------------------------------
        private bool       _isSegmentPanelOpen;
        private bool       _isLayerIndicatorVisible;
        private StoryPoint _layer;
        private StoryPoint _currentLast1Layer;
        private StoryPoint _currentLast2Layer;
        private bool       _isLayerVisible;

        
        //-------------------------------------------------------------
        //
        //          Private Methods
        //
        //-------------------------------------------------------------
        private void RefreshLayerIndicatorState()
        {
            if (LayerCollection.Count == 0)
            {
                CurrentLast1Layer = null;
                CurrentLast2Layer = null;
            }
            else if (LayerCollection.Count == 1)
            {
                CurrentLast2Layer = LayerCollection.First();
                CurrentLast1Layer = null;
            }
            else
            {
                var count = LayerCollection.Count;
                CurrentLast1Layer = LayerCollection[count - 1];
                CurrentLast2Layer = LayerCollection[count - 2];
            }

            //
            // 判断CurrentLast1Layer是否为空，如果为空的话就不显示，否则显示。
            //
            // 注意：
            // CurrentLast1Layer 与 CurrentLast2Layer 的位置关系如下所示
            //
            // +-------------------+-----+---------------------+
            // | CurrentLast2Layer |  >  |  CurrentLast1Layer  |
            // +-------------------+-----+---------------------+
            //
            IsLayerIndicatorVisible = CurrentLast1Layer is not null;
            IsLayerVisible          = Layer is not null;
        }
        
        //-------------------------------------------------------------
        //
        //          Public Methods
        //
        //-------------------------------------------------------------
        public void EnterStoryPoint(StoryPoint point)
        {
            //
            // 
            _timelineSystem.PushStoryPointLayer(point);
            RefreshStoryPointState();
        }

        //-------------------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------------------

        private void DoPopLayerCommand()
        {
            Layer = _timelineSystem.PopStoryPointLayer();
            RefreshStoryPointState();
        }

        private void DoPopAllLayerCommand()
        {
            _timelineSystem.ResetStoryPointLayer();
            RefreshStoryPointState();
        }

        private void DoGotoLayerCommand(StoryPoint point)
        {
            //
            //
            _timelineSystem.PushStoryPointLayer(point);
        }
        

        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------
        
        
        
        public StoryPoint CurrentLast2Layer
        {
            get => _currentLast2Layer;
            set => SetValue(ref _currentLast2Layer, value);
        }
        
        
        public StoryPoint CurrentLast1Layer
        {
            get => _currentLast1Layer;
            set => SetValue(ref _currentLast1Layer, value);
        }


        public StoryPoint Layer
        {
            get => _layer;
            set => SetValue(ref _layer, value);
        }
        
        public bool IsLayerIndicatorVisible
        {
            get => _isLayerIndicatorVisible;
            set => SetValue(ref _isLayerIndicatorVisible, value);
        }


        
        public bool IsLayerVisible
        {
            get => _isLayerVisible;
            set => SetValue(ref _isLayerVisible, value);
        }

        public bool IsSegmentPanelOpen
        {
            get => _isSegmentPanelOpen;
            set => SetValue(ref _isSegmentPanelOpen, value);
        }

        public ObservableCollection<StoryPoint> LayerCollection => _timelineSystem.StoryPointLayerList;
        
        //-------------------------------------------------------------
        //
        //          Commands
        //
        //-------------------------------------------------------------
        
        
        public ICommandEX PopLayerCommand    { get; }
        public ICommandEX PopAllLayerCommand { get; }
        public ICommandEX GotoLayerCommand   { get; }
    }
}